//
//  ChatBaseCell.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2020/2/7.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

let kChatAvatarWidth: CGFloat = 40                  // 头像的宽度
let kChatMargin: CGFloat = 10             // margin

protocol YSGChatCellDelegate: NSObjectProtocol {
    func chatCell(didTappedAvatar cell: ChatBaseCell)
    func chatCell(_ cell: ChatBaseCell, didTappedImage vm: ChatMsgViewModel)
    func chatRccordCellPlay(cell: ChatRecordCell)
}

class ChatBaseCell: UITableViewCell {
    
    weak var delegate: YSGChatCellDelegate?
    
    var viewModel: ChatMsgViewModel?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 头像
    lazy var avatarView: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "avatar_default"), for: .normal)
        button.addTarget(self, action: #selector(avatarViewTapped), for: .touchUpInside)
        button.layer.cornerRadius = kChatAvatarWidth / 2.0
        button.layer.masksToBounds = true
        return button
    }()
    
    lazy var sendTimeLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.textLight
        label.font = UIFont.regular(12)
        label.textAlignment = .center
        return label
    }()
    
    /// 气泡
    lazy var bubbleView = UIImageView()
    
    lazy var errorView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "send_error")
        imageView.isHidden = true
        return imageView
    }()
    
    lazy var activityView: UIActivityIndicatorView = {
        let activity = UIActivityIndicatorView(style: .gray)
        activity.hidesWhenStopped = true
        return activity
    }()
    
    func configure(with vm: ChatMsgViewModel, detailVm: AdvisoryDetailViewModel?) {
        viewModel = vm
        sendTimeLabel.text = vm.sendTime
        switch vm.userType {
        case .doctor:
            errorView.isHidden = true
            activityView.isHidden = true
            avatarView.kf.setImage(with: URL(string: detailVm?.doctorAvatar ?? ""),
                                   for: .normal,
                                   placeholder: UIImage(named: "avatar_default"))
        case .patient:
            errorView.isHidden = vm.isSendSuccess
            vm.isSending ? activityView.startAnimating() : activityView.stopAnimating()
            avatarView.kf.setImage(with: URL(string: detailVm?.detail.patientAvatar ?? ""),
                                   for: .normal,
                                   placeholder: detailVm?.avatar_default)
        }
    }
}

extension ChatBaseCell {
    func setupUI() {
        selectionStyle = .none
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        addSubview(avatarView)
        addSubview(bubbleView)
        addSubview(sendTimeLabel)
        addSubview(errorView)
        addSubview(activityView)
    }
    
    @objc func avatarViewTapped() {
        if viewModel?.userType == .doctor {
            delegate?.chatCell(didTappedAvatar: self)
        }
    }
}
